/*
 *  JDiv - API for Multimedia Application
 *  http://jdiv.sourceforge.net 
 *  ------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

package com.jdiv.functions;

import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.jdiv.JDiv;
import com.jdiv.internalClasses.JDivSound;
import com.jdiv.kernel.JDivResources;
import com.jdiv.kernel.JDivSystem;

/**Class for loading and handling Sounds.*/
public class FunctionsAudio {
	/**
	  * Load a Music to JDiv.
	  * @param strMusic File of Music.
	  * @return Number identifier of Music.
	  */
	 
	 public int loadMusic( String strMusic )
	 {
	  JDivSound jdivSound = new JDivSound(strMusic);
		
	  int musicId = jdivSound.loadMusic();
	  JDivResources.listSounds.listSong.add(musicId);
//	  return jdivSound.loadMusic();
	  return musicId;
	 }
	 
	 /** Enable sound playback. */
	 public void enableSound(){
		 JDivSystem.enableSound = true;
	 }
	 
	 /** Disable sound playback, for the following sounds after the call to this function. */
	 public void disableSound(){
		 JDivSystem.enableSound = false;
	 }
	 
	 /** Enable music playback. */
	 public void enableMusic(){
		 JDivSystem.enableMusic = true;
	 }
	 
	 /** Disable music playback, for the following sounds after the call to this function. */
	 public void disableMusic(){
		 JDivSystem.enableMusic = false;
	 }
	 
	 /**
	  * Play Music.
	  * @param idMusic Number identifier of Music.
	  * @param volume Sets volume must be given in the range [0,1] with 0 being silent and 1 being the maximum volume. 
	  * @param loop Sets whether the music stream is looping.
	  */

	 public void playMusic( int idMusic, float volume, boolean loop )
	 {
	  if( !JDivSystem.enableMusic ) return;
	  
	  JDivSound music = JDivResources.listSounds.soundGet(idMusic);
	 
	  if( music == null ){
		  JDiv.system.log("ERROR, playMusic() - Can not get music from id: " + idMusic );
		  return;
	  }
	  
	  music.playMusic(volume, loop);
	 }

	 /**
	  * Stop the Music. 
	  * @param idMusic Number identifier of Music.
	  */
	 
	 public void stopMusic( int idMusic )
	 {
	  JDivSound music = JDivResources.listSounds.soundGet(idMusic);

	  if( music == null ){
		  JDiv.system.log("ERROR, stopMusic() - Can not get music from id: " + idMusic );
		  return;
	  }

	  music.stopMusic();
	 }
	 
	 /**
	  * Pause Music.
	  * @param idMusic Number identifier of Music.
	  */

	 public void pauseMusic( int idMusic )
	 {
	  JDivSound music = JDivResources.listSounds.soundGet(idMusic);

	  if( music == null ){
		  JDiv.system.log("ERROR, pauseMusic() - Can not get music from id: " + idMusic );
		  return;
	  }

	  music.pauseMusic();
	 }
	 
	 /**
	  * Sets Music Volume.
	  * @param idMusic Number identifier of Music.
	  * @param volume The volume must be given in the range [0,1] with 0 being silent and 1 being the maximum volume. 
	  */

	 public void setMusicVolume( int idMusic, float volume )
	 {
	  JDivSound jdivSound = JDivResources.listSounds.soundGet(idMusic);

	  if( jdivSound == null ){
		  JDiv.system.log("ERROR, setMusicVolume() - Can not get music from id: " + idMusic );
		  return;
	  }
	  
	  if( jdivSound.music != null )
		  jdivSound.music.setVolume(volume);
	 }
	 
	 /**  
	  * @param idMusic Number identifier of Music.
	  * @return True if playing Music, or False if not.
	  */

	 public boolean isMusicPlaying( int idMusic )
	 {
	  JDivSound jdivSound = JDivResources.listSounds.soundGet(idMusic);

	  if( jdivSound == null ){
		  JDiv.system.log("ERROR, isMusicPlaying() - Can not get music from id: " + idMusic );
		  return false;
	  }

	  if( jdivSound.music != null )
	   return jdivSound.music.isPlaying();
	  
	  return false;
	 }
	 
	 /**
	  * Gets the Music stored in JDiv.
	  * @param idMusic Number identifier of Music.
	  * @return Music from LibGdx.
	  */

	 public Music getMusic( int idMusic )
	 {
		 
	  JDivSound jdivSound = JDivResources.listSounds.soundGet(idMusic);

	  if( jdivSound == null ){
			  JDiv.system.log("ERROR, getMusic() - Can not get music from id: " + idMusic );
			  return null;
	  }
		  
	  return jdivSound.music;
	 }
	 
	 /**
	  * Load a Sound to JDiv.
	  * @param strSound File of Sound.
	  * @return Number identifier of Sound.
	  */
	 
	 public int loadSound( String strSound )
	 {
	  JDivSound jdivSound = new JDivSound(strSound);
		 
	  return jdivSound.loadSound();
	 }
	 
	 /**
	  * Play Sound.
	  * @param idSound Number identifier of Sound.
	  * @param volume Sets volume must be given in the range [0,1] with 0 being silent and 1 being the maximum volume.
	  */

	 public void playSound( int idSound, float volume )
	 {
	  playSound(idSound, volume, 1, 0);
	 }
	 
	 /**
	  * Play Sound.
	  * @param idSound Number identifier of Sound.
	  * @param volume Sets volume must be given in the range [0,1] with 0 being silent and 1 being the maximum volume.
	  * @param pitch The pitch multiplier, 1 == default, >1 == faster, <1 == slower, the value has to be between 0.5 and 2.0.
	  * @param pan Panning in the range -1 (full right) to 1 (full left). 0 is center position.
	   */

	 public void playSound( int idSound, float volume, float pitch, float pan )
	 {
	  if( !JDivSystem.enableSound ) return;
		 
	  JDivSound sound = JDivResources.listSounds.soundGet(idSound);
		 
	  if( sound == null ){
		  JDiv.system.log("ERROR, playSound() - Can not get sound from id: " + idSound );
		  return;
	  }
	  
	  sound.playSound(volume, pitch, pan, false);
	 }
	 
	 /**
	 * Play the Sound in loop.
	 * @param idSound Number identifier of Sound.
	 * @param volume Sets volume must be given in the range [0,1] with 0 being silent and 1 being the maximum volume.
	 */
	 
	 public void playSoundLoop( int idSound, float volume )
	 {
	  playSoundLoop( idSound, volume, 1, 0 );
	 }
	 
	 /**
	  * Play the Sound in Loop.
	  * @param idSound Number identifier of Sound.
	  * @param volume Sets volume must be given in the range [0,1] with 0 being silent and 1 being the maximum volume.
	  * @param pitch The pitch multiplier, 1 == default, >1 == faster, <1 == slower, the value has to be between 0.5 and 2.0.
	  * @param pan Panning in the range -1 (full right) to 1 (full left). 0 is center position.
	  */

	 public void playSoundLoop( int idSound, float volume, float pitch, float pan )
	 {
	  if( !JDivSystem.enableSound ) return;
		 
	  JDivSound sound = JDivResources.listSounds.soundGet(idSound);
		 
	  if( sound == null ){
		  JDiv.system.log("ERROR, playSound() - Can not get sound from id: " + idSound );
		  return;
	  }
	  
	  sound.playSound(volume, pitch, pan, true);
	 }
	  
	 /**
	  * Stop the Sound.
	  */

	 public void stopSound( int idSound )
	 {
		 JDivSound sound = JDivResources.listSounds.soundGet(idSound);

		  if( sound == null ){
			  JDiv.system.log("ERROR, stopSound() - Can not get sound from id: " + idSound );
			  return;
		  }

		  sound.stopSound();
	 }
	  
	 /**
	  * Set volume, pith and panning from Voice of Sound.
	  * @param idSound Number identifier of Sound.
	  * @param volume Sets volume must be given in the range [0,1] with 0 being silent and 1 being the maximum volume.
	  * @param pitch The pitch multiplier, 1 == default, >1 == faster, <1 == slower, the value has to be between 0.5 and 2.0.
	  * @param pan Panning in the range -1 (full right) to 1 (full left). 0 is center position.
	  */

	 public void setSound( int idSound, float volume, float pitch, float pan )
	 {
	  JDivSound sound = JDivResources.listSounds.soundGet(idSound);

	  if( sound == null ){
		  JDiv.system.log("ERROR, setSound() - Can not get sound from id: " + idSound );
		  return;
	  }

	  sound.setVolumePitchPan(volume, pitch, pan);
	 }
	  
	 /**
	  * Gets the Sound stored in JDiv.
	  * @param idSound Number identifier of Sound.
	  * @return Sound from libgdx.
	  */

	 public Sound getSound( int idSound )
	 {
		  JDivSound jdivSound = JDivResources.listSounds.soundGet(idSound);

		  if( jdivSound == null ){
				  JDiv.system.log("ERROR, getSound() - Can not get sound from id: " + idSound );
				  return null;
		  }
			  
		  return jdivSound.sound;
	 }
	 
	 /**
	   * Unload Sound stored in JDiv. Be careful when using this function, if you unload the sound can no longer be reused.
	   * @param idSound Number identifier of Sound.
	   */
	 
	 public void unloadSound( int idSound ){
		 JDivResources.listSounds.soundDispose(idSound);
	 }
	 
	 /**
	   * Unload Music stored in JDiv. Be careful when using this function, if you unload the music can no longer be reused.
	   * @param idMusic Number identifier of Music.
	   */
	 
	 public void unloadMusic( int idMusic ){
		 JDivResources.listSounds.soundDispose(idMusic);
	 }
	 
	 // Compatibilidad con Bennu
	 public int playWav(int idSound, int loop) {
		 
		 if (loop < 0) {
		
			 this.playSoundLoop(idSound, 100);
			 
		 } else {
			 
			 this.playSound(idSound, 100);

		 }
		 
		 return 0;
	 }

	 // Compatibilidad con Bennu
	 public int playSong(int idMusic, int loop) {
		 
		 this.playMusic(idMusic, 100, (loop < 0) ? true : false);
		 
		 return 0;
		 
	 }
	 
	 public void pauseSong() {
		 for (int i = 0; i < JDivResources.listSounds.listSong.size(); i++) {
			 JDiv.audio.pauseMusic(JDivResources.listSounds.listSong.get(i));
		 }
	 }
	 
	 public void resumeSong() {
		 
		 for (int i = 0; i < JDivResources.listSounds.listSong.size(); i++) {
			 JDiv.audio.playSong(JDivResources.listSounds.listSong.get(i),-1);
		 }
		 
	 }
	 	 

}
